﻿using System;
using System.Net;
using System.Net.Mail;
using Devonline.Core;

namespace Devonline.Utils
{
    /// <summary>
    /// SMTP send email class
    /// </summary>
    public class SmtpMail
    {
        /// <summary>
        /// Smtp Host Address
        /// </summary>
        public string Host { get; set; }
        /// <summary>
        /// Port Number
        /// </summary>
        public int Port { get; set; }
        /// <summary>
        /// From
        /// </summary>
        public string From { get; set; }
        /// <summary>
        /// To
        /// </summary>
        public string To { get; set; }
        /// <summary>
        /// CC
        /// </summary>
        public string CC { get; set; }
        /// <summary>
        /// Bcc
        /// </summary>
        public string Bcc { get; set; }
        /// <summary>
        /// Body
        /// </summary>
        public string Body { get; set; }
        /// <summary>
        /// Subject
        /// </summary>
        public string Subject { get; set; }
        /// <summary>
        /// Is Html
        /// </summary>
        public bool IsHtml { get; set; }
        /// <summary>
        /// Attachment File Names
        /// </summary>
        public string AttachmentFileNames { get; set; }
        /// <summary>
        /// Use Credentials
        /// </summary>
        public bool UseCredentials { get; set; }
        /// <summary>
        /// UserName
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        /// Password
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// Send an email
        /// </summary>
        public void Send()
        {
            try
            {
                if (Port <= 0 || Port > 65535)
                {
                    Port = 25;
                }

                using var smtpClient = new SmtpClient(Host, Port);
                using var mailMessage = new MailMessage
                {
                    From = new MailAddress(From),
                    Body = Body,
                    Subject = Subject,
                    IsBodyHtml = IsHtml
                };

                if (To.IsNotNullOrEmpty())
                {
                    foreach (string to in To.Split(AppSettings.CHAR_SEMICOLON, StringSplitOptions.RemoveEmptyEntries))
                    {
                        mailMessage.To.Add(to);
                    }
                }

                if (CC.IsNotNullOrEmpty())
                {
                    foreach (string cc in CC.Split(AppSettings.CHAR_SEMICOLON, StringSplitOptions.RemoveEmptyEntries))
                    {
                        mailMessage.CC.Add(cc);
                    }
                }

                if (Bcc.IsNotNullOrEmpty())
                {
                    foreach (string bcc in Bcc.Split(AppSettings.CHAR_SEMICOLON, StringSplitOptions.RemoveEmptyEntries))
                    {
                        mailMessage.Bcc.Add(bcc);
                    }
                }

                if (AttachmentFileNames.IsNotNullOrEmpty())
                {
                    foreach (string attachment in AttachmentFileNames.Split(AppSettings.CHAR_SEMICOLON, StringSplitOptions.RemoveEmptyEntries))
                    {
                        mailMessage.Attachments.Add(new Attachment(attachment));
                    }
                }

                if (UseCredentials)
                {
                    var insNetworkCredential = new NetworkCredential(UserName, Password);
                    smtpClient.Credentials = insNetworkCredential;
                }

                smtpClient.Send(mailMessage);
            }
            catch (Exception ex)
            {
                Logger.Exception("邮件发送异常, 请查看详情: ", ex);
            }
        }
    }
}
